using System;
using System.Collections.Generic;
using System.Text;
using MULENetworking;

namespace MULEServer
{
    class GameFlow
    {
        private enum engineStates
        {
            TitleScreen = 0, ChoosePlot = 1, Waiting = 2, PlayerTurn = 3,
            Auction = 4, Standings = 5, GameOver = 6, House = 7
        }
        private RandomEvent randomEvent;
        private Production production;
        private UsageSpoilage useSpoil;

        GameFlow(int level, IPlayer player1, IPlayer player2, IPlayer player3, IPlayer player4, IGame game, Store store)
        {
            randomEvent = new RandomEvent();
            production = new Production();
            useSpoil = new UsageSpoilage();
            //beginning: ship flys in, drops off players.

            //for loop begins, iterates for 6 times (beginner), 12 times (standard, tourney)
            int totalTurns = 0;
            if (level == 0) totalTurns = 6;
            if (level == 1 || level == 2) totalTurns = 12;
            for (int turn = 1; turn <= totalTurns; turn++)
            {   

                //land selection phase
                game.CurrentTurn = turn;
                game.GameState = (int)engineStates.ChoosePlot; // set state to landSelection


                // if(standard || tourney) land auction phase
                if (level == 1 || level == 2)
                {
                    // determine whihc plot of land will be auctioned, if any

                    // show the plot of land that is up for auction on the map

                    // go to auction phase
                }

                /* USELESS
                //find the order the players should go in, then set up a loop.
                IPlayer[] playerOrder = new IPlayer[4];
                int position = player1.place;
                playerOrder[position] = player1;
                position = player2.place;
                playerOrder[position] = player2;
                position = player3.place;
                playerOrder[position] = player3;
                position = player4.place;
                playerOrder[position] = player4;
                // player that is in last gets to go first, goes in reverse order
                game.GameState = (int)engineStates.PlayerTurn;
                for (int x = 3; x <= 0; x--)
                {
                    // set which player is active
                    IPlayer currentPlayer = playerOrder[x];
                    currentPlayer.myTurn = true;
                    //game.CurrentPlayer = *** what is the implementation for this? ***
                    // random player event
                    String displayString = randomEvent.generatePlayerEvent(playerOrder[x], turn);


                    // mule phase: need a way to know when the mule phase is over.


                    currentPlayer.myTurn = false;
                }
                 * */

                // production phase
                    // colony random event **NYI**
                    // production
                /*
                
                */
                    // animation or something, or not :p


                // auction phase

                    // run usage/spoilage calculations for all players
                int useless;
                useless = useSpoil.foodUsage((IPlayer)game.Player1);
                useless = useSpoil.energyUsage((IPlayer)game.Player1);

                useless = useSpoil.foodUsage((IPlayer)game.Player2);
                useless = useSpoil.energyUsage((IPlayer)game.Player2);

                useless = useSpoil.foodUsage((IPlayer)game.Player3);
                useless = useSpoil.energyUsage((IPlayer)game.Player3);

                useless = useSpoil.foodUsage((IPlayer)game.Player4);
                useless = useSpoil.energyUsage((IPlayer)game.Player4);

                useless = useSpoil.foodSpoilage((IPlayer)game.Player1);
                useless = useSpoil.energySpoilage((IPlayer)game.Player1);
                useless = useSpoil.smithoreSpoilage((IPlayer)game.Player1);

                useless = useSpoil.foodSpoilage((IPlayer)game.Player2);
                useless = useSpoil.energySpoilage((IPlayer)game.Player2);
                useless = useSpoil.smithoreSpoilage((IPlayer)game.Player2);

                useless = useSpoil.foodSpoilage((IPlayer)game.Player3);
                useless = useSpoil.energySpoilage((IPlayer)game.Player3);
                useless = useSpoil.smithoreSpoilage((IPlayer)game.Player3);

                useless = useSpoil.foodSpoilage((IPlayer)game.Player4);
                useless = useSpoil.energySpoilage((IPlayer)game.Player4);
                useless = useSpoil.smithoreSpoilage((IPlayer)game.Player4);

                game.Player1.food += production.produceFood((IPlayer)game.Player1);
                game.Player1.energy += production.produceEnergy((IPlayer)game.Player1);
                game.Player1.smithore += production.produceSmithore((IPlayer)game.Player1);

                game.Player2.food += production.produceFood((IPlayer)game.Player2);
                game.Player2.energy += production.produceEnergy((IPlayer)game.Player2);
                game.Player2.smithore += production.produceSmithore((IPlayer)game.Player2);

                game.Player3.food += production.produceFood((IPlayer)game.Player3);
                game.Player3.energy += production.produceEnergy((IPlayer)game.Player3);
                game.Player3.smithore += production.produceSmithore((IPlayer)game.Player3);

                game.Player4.food += production.produceFood((IPlayer)game.Player4);
                game.Player4.energy += production.produceEnergy((IPlayer)game.Player4);
                game.Player4.smithore += production.produceSmithore((IPlayer)game.Player4);

                // set state to auction
                game.GameState = (int)engineStates.Auction;

                    // smithore auction

                    // food auction

                    // energy auction

                    // crystite auction
                // when all players are in waiting state, move to next phase

                // calculate and show score
                NetworthCalculator nwcalc = new NetworthCalculator(player1, player2, player3, player4, store);

            } //end for loop

            // show overall stats for game

            // when players leave the game, go back to chat room
            // when all players are gone, shut down this thread
        }
    }
}
